package com.gec.controller;

import java.util.Map;

/**
 * @author Aniki
 * @date  2021-12-14
 */
public class PageData {
    private Integer page;
    private Integer limit;
    private Map<String,String> keywords;

     public PageData(Map data) {
         this.page = getInteger(data, "page", 1);
         this.limit = getInteger(data, "limit", 10);
         data.remove("page");
         data.remove("limit");
         keywords = data;
     }

     private Integer getInteger(Map<String,String>data,String key,Integer defVal){
         String regex = "\\d+";

         String val = data.get(key);
         if (val == null) {
             return defVal;
         }
         if (!val.matches(regex)) {
             return defVal;
         }
         return Integer.valueOf(val);
     }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public Map<String, String> getKeywords() {
        return keywords;
    }

    public void setKeywords(Map<String, String> keywords) {
        this.keywords = keywords;
    }

    @Override
    public String toString() {
        return "PageData{" +
                "page=" + page +
                ", limit=" + limit +
                ", keywords=" + keywords +
                '}';
    }
}
